package com.taobao.machinesystem.common;

import lombok.Data;

import java.util.HashMap;
import java.util.Map;

/**
 * 统一返回 对象
 */
@Data
public class Result {

    private Integer code;
    private String message;
    private Map<String, Object> map = new HashMap<>();


    public Integer getCode() {
        return code;
    }

    public Result setCode(Integer code) {
        this.code = code;
        return this;
    }

    public String getMessage() {
        return message;
    }

    public Result setMessage(String message) {
        this.message = message;
        return this;
    }

    public Map<String, Object> getMap() {
        return map;
    }

    public Result setMap(Map<String, Object> map) {
        this.map = map;
        return this;
    }

    public static Result ok() {
        Result result = new Result();
        result.setCode(1);
        result.setMessage("操作成功");
        return result;
    }


    public Result setMapValue(String key, Object value) {
        Map<String, Object> map = this.getMap();
        if (map == null) {
            map = new HashMap<>();
        }
        map.put(key, value);
        return this;
    }


    public static Result error() {
        Result result = new Result();
        result.setCode(-1);
        result.setMessage("操作失败，联系管理员！");
        return result;
    }

    @Override
    public String toString() {
        return "Result{" +
                "code=" + code +
                ", message='" + message + '\'' +
                ", map=" + map +
                '}';
    }
}

